﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bainx.Extend
{
    /// <summary>
    /// 类型转换
    /// </summary>
    public static class TypeConvert
    {
        /// <summary>
        /// 将字符串转换成布尔值
        /// </summary>
        /// <param name="argv">字符串：True,true,TRUE,1</param>
        /// <returns>布尔值</returns>
        public static bool ToBoolean(this string argv)
        {
            bool ret = false;
            try
            {
                switch (argv)
                {
                    case "true":
                    case "True":
                    case "TRUE":
                    case "1":
                        ret = true;
                        break;
                    default:
                        ret = false;
                        break;
                }
            }
            catch 
            {
                ret = false;
            }
            return ret;
        }
        /// <summary>
        /// 将对象类型转换成布尔值，异常时返回false
        /// </summary>
        public static bool ToBoolean(this object argv)
        {
            try { return Convert.ToBoolean(argv); }
            catch { return false; }
        }

        /// <summary>
        /// 将字符串MD5加密
        /// </summary>
        /// <param name="argv">明文</param>
        /// <returns>密文</returns>
        public static string ToMD5(this string argv)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(argv, "MD5").ToLower();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="argv"></param>
        /// <param name="exceptionValue"></param>
        /// <returns></returns>
        public static int ToInt(this string argv,int exceptionValue)
        {
            try { return Convert.ToInt32(argv); }
            catch { return exceptionValue; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="argv"></param>
        /// <returns></returns>
        public static int ToInt(this string argv)
        {
            try { return Convert.ToInt32(argv); }
            catch { return 0; }
        }

        /// <summary>
        /// 字符串转换成时间DateTime类型，异常时返回exceptionValue
        /// </summary>
        public static DateTime ToDateTime(this string argv, DateTime exceptionValue)
        {
            try { return Convert.ToDateTime(argv); }
            catch { return exceptionValue; }
        }
        /// <summary>
        /// 字符串转换成时间DateTime类型，异常时返回2010-1-1 0:0:0
        /// </summary>
        public static DateTime ToDateTime(this string argv)
        {
            try { return Convert.ToDateTime(argv); }
            catch { return new DateTime(2010,1,1,0,0,0); }
        }

        /// <summary>
        /// 布尔值转短整形
        /// </summary>
        public static short ToShort(this bool argv)
        {
            return Convert.ToInt16(argv);
        }

        

    }
}
